﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.DataModel;
using Sausage.Web.Models;

namespace Sausage.Web.Domain
{
    public class MaintainCategoryHelper 
    {
        public static IEnumerable<TreeNodeData> QueryMaintainCategoryTree()
        {
            using (var context = new SausageEntities())
            {
                return GetChildren(null, context.MaintainCategory.ToList());
            }
        }

        private static IEnumerable<TreeNodeData> GetChildren(Guid? parentID, IEnumerable<MaintainCategory> maintainCategoryList)
        {
            var children = from maintainCategory in maintainCategoryList
                           where maintainCategory.ParentID == parentID
                           orderby maintainCategory.CategoryName
                           select
                               new TreeNodeData
                               {
                                   id = maintainCategory.CategoryID,
                                   expanded = false,
                                   leaf = maintainCategory.IsLeaf,
                                   name = maintainCategory.CategoryName,
                                   parentID = maintainCategory.ParentID,
                                   unit = maintainCategory.Unit,
                                   pinyin = maintainCategory.CategoryPinYin,
                                   data = GetChildren(maintainCategory.CategoryID, maintainCategoryList)
                               };

            return children;
        }

        public static MaintainCategory GetMaintainCategoryByID(Guid id)
        {
            using (var context = new SausageEntities())
            {
                return context.MaintainCategory.FirstOrDefault(maintainCategory => maintainCategory.CategoryID == id);
            }
        }

        public static DeleteResult DeleteMaintainCategory(Guid id)
        {
            using (var context = new SausageEntities())
            {
                var maintainCategory = context.MaintainCategory.FirstOrDefault(item => item.CategoryID == id);
                if (maintainCategory == null || !maintainCategory.IsLeaf)
                    return DeleteResult.Ok;

                //just delete the leaf node.
                if (maintainCategory.ParentID.HasValue)
                {
                    var brothers = context.MaintainCategory.Where(item => item.ParentID == maintainCategory.ParentID).ToList();
                    if (brothers.Count <= 1)
                    {
                        var parent = context.MaintainCategory.FirstOrDefault(item => item.CategoryID == maintainCategory.ParentID.Value);
                        if (parent != null)
                            parent.IsLeaf = true;
                    }
                }

                context.ObjectStateManager.ChangeObjectState(maintainCategory, EntityState.Deleted);
                try
                {
                    context.SaveChanges();
                }
                catch (UpdateException)
                {
                    return DeleteResult.UsingError;
                }
                return DeleteResult.Ok;
            }
        }

        public static void UpdateMaintainCategory(TreeNodeData data)
        {
            using (var context = new SausageEntities())
            {
                var maintainCategory = context.MaintainCategory.FirstOrDefault(item => item.CategoryID == data.id);
                if (maintainCategory == null)
                    return;

                maintainCategory.CategoryName = data.name;
                maintainCategory.CategoryPinYin = data.pinyin;
                maintainCategory.Unit = data.unit;

                context.SaveChanges();
            }
        }

        public static MaintainCategory GetMaintainCategoryByName(string name)
        {
            using (var context = new SausageEntities())
            {
                return context.MaintainCategory.FirstOrDefault(maintainCategory => maintainCategory.CategoryName == name);
            }
        }

        public static void CreateMaintainCategory(TreeNodeData data)
        {
            using (var context = new SausageEntities())
            {
                var maintainCategory = new MaintainCategory();

                maintainCategory.CategoryID = Guid.NewGuid();
                maintainCategory.ParentID = data.parentID;
                maintainCategory.CategoryName = data.name;
                maintainCategory.CategoryPinYin = data.pinyin;
                maintainCategory.Unit = data.unit;
                maintainCategory.TheOrder = 0;
                maintainCategory.IsLeaf = true;

                if (data.parentID.HasValue)
                {
                    var parent = context.MaintainCategory.FirstOrDefault(item => item.CategoryID == data.parentID.Value);
                    parent.IsLeaf = false;
                }

                context.AddToMaintainCategory(maintainCategory);
                context.SaveChanges();
            }
        }

    }
}
